/*
 * @Author: Crism
 * @Date: 2018-07-31 17:56:48
 * @LastEditors: Crism
 * @LastEditTime: 2018-08-02 17:47:10
 * @Description: 
 * @Email: crism@qq.com
 */

const cheerio = require('cheerio');
const ywurl = 'https://so.gushiwen.org/shiwen2017/ajaxfanyi.aspx?id=';
module.exports.getTitleList = getTitleList;
module.exports.getDetail = getDetail;
module.exports.printInfo = printInfo;
module.exports.getDataTest = getDataTest;

/* 过滤页面信息 */
async function getTitleList(html) {

    if (html) {

        let $ = cheerio.load(html, { decodeEntities: false });

        let List = $('div.sons');

        let ListData = [];

        let a = 0;

        List.find('a').each(function (item) {
            let pic = $(this);
            if ('更多》' != pic.html()) {
                ListData.push({
                    'title': pic.html(),
                    'url': pic.attr('href'),
                });
                a++;
            }
        });
        console.log("总共抓取: " + a + " 条数据");
        return ListData;
    } else {
        console.log('无数据传入！');
    }
}

/* 获取推荐文言文详情 */
async function getDetail(html) {

    if (html) {
        let $ = cheerio.load(html.toString(), { decodeEntities: false });

        let List = $('div.sons');
        let ListData = [];

        let content = '';
        List.find('div.contson').find('p').each(function (i, elem) {
            content += $(this).text();
        });
        if (content.length == 0 || content.length < 20) {
            content = List.find('div.contson').html();
        }

        ListData.push({
            '题目': List.find('h1').html(),
            '朝代': List.find('p').find('a').html(),
            '作者': List.find('p').find('a').next().next().html(),
            '内容': content = content != null ? content.replace(/^\s+|\s+$/g, "") : content,
            // '译文': yiuwen,
            // '注释': zhushi,
        })

        // console.log(ListData);
        return ListData;
    } else {
        console.log('无数据传入！');
    }
}

/* 打印信息 */
async function printInfo(ListData) {
    // 计数
    let count = 0;
    // 遍历信息列表
    ListData.forEach(function (item) {
        // 获取图片
        let pic_src = item.pic_src;
        // 获取图片对应的链接地址
        let pic_href = item.pic_href;
        // 获取图片信息
        let pic_message = item.pic_message;
        // 打印信息
        console.log('第' + (++count) + '个轮播图');
        console.log(pic_message);
        console.log(pic_href);
        console.log(pic_src);
        console.log('\n');
    });
}

/* 测试 */
async function getDataTest(html) {
    if (html) {
        let $ = cheerio.load(html.toString(), { decodeEntities: false });

        let Lists = $('div.sons');
        let ListData = [];

        let content = '';
        Lists.find('div.contson').find('p').each(function (i, elem) {
            content += $(this).text();
        });
        if (content.length == 0 || content.length < 20) {
            content = Lists.find('div.contson').html();
        }

        ListData.push({
            '题目': Lists.find('h1').html(),
            '朝代': Lists.find('p').find('a').html(),
            '作者': Lists.find('p').find('a').next().next().html(),
            '内容': content,
            // '译文': yiuwen,
            // '注释': zhushi,
        })

        return ListData;
    } else {
        console.log('无数据传入！');
    }
}
